﻿// bacth, 10:30 AM 7/11/2008
var contextMenuInterval = null;
var newsid;
function newslist_cboIsHot_selectedIndexChange(e)
{
	var newsID = 0;
	var tr = e.parentNode;
	while(tr.nodeName.toLowerCase() != 'tr') tr = tr.parentNode;
	newsID = tr.getElementsByTagName('input')[0].value;
	hdArgs.value = newsID + ',' + e.options[e.selectedIndex].value;
	commandName = 'setloaitin';
	__doPostBack(btnSetLoaiTin.name,'');
}
function newslist_chkIsFocus_CheckedChanged(e)
{
	var newsID = 0;
	var tr = e.parentNode;
	while(tr.nodeName.toLowerCase() != 'tr') tr = tr.parentNode;
	newsID = tr.getElementsByTagName('input')[0].value;
	hdArgs.value = newsID + ',' + e.checked;
	commandName = 'settieudiem';
	
	__doPostBack(btnSetTieuDiem.name,'');
}
function newslist_setstyleforcheckbox()
{
	/*var chk, chks =  document.getElementById(grdListNewsID).getElementsByTagName('input');
	var i = 0, count = chks.length;
	var td;
	for (i=1; i<count; i++)
	{
		chk = chks[i];
		if (chk.type.toLowerCase() == 'checkbox')
		{
			td = chk.parentNode; while (td.nodeName.toLowerCase() != 'td') td = td.parentNode;
			if (chk.checked)
				td.className = 'active'; 
			else
				td.className = '';
			
		}
	}*/
}

function newslist_removeActiveRow(sender, eventArgs)
{
	if (eventArgs.get_error() != undefined && eventArgs.get_error().httpStatusCode == '500')
	{
		 var errorMessage = eventArgs.get_error().message;
        eventArgs.set_errorHandled(true);
		alert(errorMessage);
		return;
	}
	document.getElementById('contextmenu').style.display = 'none';
	switch (commandName)
	{
		case 'xoatam':
			tr.parentNode.removeChild(tr); break;
		case 'xuatban':
			tr.parentNode.removeChild(tr); break;
		case 'xoathat':
			tr.parentNode.removeChild(tr); break;
		case 'guilen':
			tr.parentNode.removeChild(tr); break;
		case 'trave':
			tr.parentNode.removeChild(tr); break;
		case 'gobo':
			tr.parentNode.removeChild(tr); break;
		case 'selectedRow':
			var chks = document.getElementById(grdListNewsID).getElementsByTagName('input');
			var trActive = null;
			var trs = new Array();
			var i=0;
			for (i=1; i<chks.length; i++)
			{
				if (chks[i].type.toLowerCase() == 'checkbox' && chks[i].id.indexOf('chkSelect')>= 0 && chks[i].checked)
				{
					trActive = chks[i].parentNode;
					while (trActive.nodeName.toLowerCase() != 'tr') trActive = trActive.parentNode;
					trs.push(trActive);
				}
			}
			if (trs.length>0)
			{
				var trBody = trs[0].parentNode;
				for (i=0; i<trs.length; i++) trBody.removeChild(trs[i]);
			}
			break;
		case 'settieudiem': break;
		case 'setloaitin': break;
		default: animate_scrollToTop();
			break;
	}
	commandName = null;
}

function newslist_grid_clickitem(ev, a, newsID, catID)
{

	hdNewsID.value = newsID;
	hdCat_ID.value = catID;
	newsid = newsID;
	
	var contextmenu = document.getElementById('contextmenu');
	contextmenu.style.display = 'block';
	contextMenuInterval = setTimeout('document.getElementById(\'contextmenu\').style.display = \'none\';', 5000);
	
	
	var left = findPosX(a) + a.offsetWidth - contextmenu.offsetWidth;
	var top = findPosY(a) + a.offsetHeight;
	
	contextmenu.style.top = top + 'px';
	contextmenu.style.left = left + 'px';
	
	tr = a.parentNode;	
	while (tr.nodeName.toLowerCase() != 'tr') tr = tr.parentNode;	
}
function newslist_setstylefordropdownlist()
{
	/*var cbo, cbos =  document.getElementById(grdListNewsID).getElementsByTagName('select');
	var i = 0, index = 0, count = cbos.length;
	var td;
	for (i=0; i<count; i++)
	{
		cbo = cbos[i];
		if (cbo.type.toLowerCase() == 'select-one')
		{
			td = cbo.parentNode; while (td.nodeName.toLowerCase() != 'td') td = td.parentNode;
			index = cbo.selectedIndex;
			if (index == 0)
			{
				td.className = ''; 
				cbo.className = '';	 
			}
			else
			{
				td.className = 'active'; 
				cbo.className = 'active';
			}
		}
	}*/
}
function checkMultiAction(action)
{
	var chks = document.getElementById(grdListNewsID).getElementsByTagName('input');
	
	commandName = 'selectedRow';
	
	var newsIDs = new Array();
	
	for (var i=1; i<chks.length; i++)
	{
		if (chks[i].type.toLowerCase() == 'checkbox' && chks[i].id.indexOf('chkSelect')>= 0 && chks[i].checked)
		{
			newsIDs.push(chks[i].value);
		}	
	}
	
	if (newsIDs.length == 0)
	{
		alert('Bạn chưa chọn tin');
		return false;
	}
	
	hdNewsID.value = newsIDs.join(',');
	hdArgs.Value = newsIDs.join(',');
	
	switch (action)
	{
		case 'send': return confirm('Bạn có muốn gửi tin đã chọn hay không?'); break;
		case 'sendback': return confirm('Bạn có muốn trả lại bài viết đã chọn hay không?'); break;
		case 'approved': return confirm('Bạn có muốn xuất bản tin đã chọn hay không?'); break;
		case 'disapproved': return confirm('Bạn có muốn gỡ tin đã chọn hay không?'); break;
		case 'delete': return confirm('Bạn có muốn xóa tin đã chọn hay không?'); break;
	}
	return false;
}

function setInactiveClass(tblID)
{
	var trs = document.getElementById(grdListNewsID).getElementsByTagName('tr');
	for (var i=1; i<trs.length; i++)
	{
		if (trs[i].className) trs[i].setAttribute('_inactive', trs[i].className);
	}
}

function CloseComment()
{
	document.getElementById('divShowComment').style.display = 'none';
	document.getElementById("bgFilter").style.display = 'none';
}
function ClosePop()
{
	document.getElementById('loginForm').style.display = 'none';
	document.getElementById("bgFilter").style.display = 'none';
}
function newslist_mouseup(evt)
{
	document.getElementById('contextmenu').style.display = 'none';
	
	evt = evt || window.event;
	var tar = evt.target || evt.srcElement;
	
	/*if (tar.nodeName.toLowerCase() == 'td' && tar.parentNode.parentNode.parentNode.id == grdListNewsID)
	{
		var chks = tar.getElementsByTagName('input');
		for (var i=0; i<chks.length; i++)
		{
			if (chks[i].type.toLowerCase() == 'checkbox')
			{
				chks[i].checked = !chks[i].checked;
				if (chks[i].id.indexOf('chkSelect') >= 0)
					tar.parentNode.className = chks[i].checked ? 'active2' : tar.parentNode.getAttribute('_inactive'); // select row
				else if (chks[i].id.indexOf('chkIsFocus') >= 0)
					newslist_chkIsFocus_CheckedChanged(chks[i]); // set tieu diem
			}	
		}
	}*/
	if (contextMenuInterval)
	{
		clearTimeout(contextMenuInterval);
		contextMenuInterval = null;
	}
}

function editnews(news_id)
{
    hdNewsID.value = news_id
    suanoidung();
    return false;
}

//*****************************************************************************************//
// context menu for news list
function xemtruoc()
{

	if (hdNewsID != null && !isNaN(Number(hdNewsID.value)))
	openpreview(linkpreview + hdNewsID.value,900,700);

}

function xemtruoc(newid)
{
    var url ="";
    if(newid!=null)
        url = linkpreview + newid;
    else
        url = linkpreview + newsid;
	openpreview(url,900,700);
}
function suanoidung()
{
	var hr=window.location.href.substring(window.location.href.lastIndexOf("/")+1,window.location.href.length).replace(".chn","").replace('#a',''); 
    hr = hr.replace('#','');
    if(hr.indexOf('sendlist')>=0)
        return;
    var Page = "add,"+hr+"/" + hdNewsID.value +".chn";
    if(hr.indexOf('office')>=0)
        Page=hr+"/add/"+ hdNewsID.value +".chn";
    window.location = Page;
}
function gobo()
{
	if (confirm('Bạn có muốn gỡ bỏ bài này?'))
	{
	    var catPer = hdCatPer.value;
	    
	    if(catPer.indexOf(hdCat_ID.value)>=0)
	    {
		    hdArgs.value = hdNewsID.value + ',7,' +  hdCat_ID.value;
		    __doPostBack(btnUpdateStatus.name,'');
		    commandName = 'gobo';
		}
		else
		{
		    alert("Bạn không có quyền gỡ bỏ bài này!");
		}
	}
}
function xuatban()
{
	hdArgs.value = hdNewsID.value + ',3';
	__doPostBack(btnUpdateStatus.name,'');
	commandName = 'xuatban';
}
function xemthongtinbandocgui()
{
	openpreview('/ThongTinBanDocGui.aspx?NewsID=' + hdNewsID.value, 400, 300);
}
function xemnhanxet()
{
	openpreview('/CommentNewsReturn.aspx?News_ID=' + hdNewsID.value, 600, 230);
}
function trave()
{
	showModalPopup('feedbackform');
	hdID.value = hdNewsID.value;
	commandName = 'trave';
}
function xoatam()
{

	if (confirm('Bạn có muốn xóa tạm tin đã chọn hay không?'))
	{
	    var catPer = hdCatPer.value;
	    
	    if(catPer.indexOf(hdCat_ID.value)>=0)
	    {
		    hdArgs.value = hdNewsID.value + ',6';
		    __doPostBack(btnUpdateStatus.name,'');
		    commandName = 'xoatam';
		}
		else
		{
		    alert("Bạn không có quyền xóa tạm tin này!");
		}
	}
}
function xoathat()
{
	if (confirm('Bạn có muốn xóa tin đã chọn hay không?'))
	{
	    var catPer = hdCatPer.value;
	    
	    if(catPer.indexOf(hdCat_ID.value)>=0)
	    {
		    hdArgs.value = hdNewsID.value
		    __doPostBack(btnDeletePermanently.name,'');
		    commandName = 'xoathat';
		}
		else
		{
		     alert("Bạn không có quyền xóa tin này!");
		}
	}
}
function guilen()
{
	hdArgs.value = hdNewsID.value + ',' + (isSendDirectly ? 2 : 1); // neu duoc gui truc tiep thi => cho duyet, neu ko => bien tap
	commandName = 'guilen';
	__doPostBack(btnUpdateStatus.name,'');
}
function init_contextmenu()
{
	var url = window.location.href;
	if (url.indexOf('publishbycat.chn')>0)
		showcontextmenu(new Array(0,1,2));
	else if (url.indexOf('publishedlist.chn')>0)
		showcontextmenu(new Array(0,1,2));
	else if (url.indexOf('removedlist.chn')>0)
		showcontextmenu(new Array(0,1,5,7,8));
	else if (url.indexOf('templist.chn')>0)
		showcontextmenu(new Array(0,1,5,6));
	else if (url.indexOf('editwaitlist.chn')>0)
		showcontextmenu(new Array(0,1,5,7,6));
	else if (url.indexOf('editinglist.chn')>0)
		showcontextmenu(new Array(0,1,5,7,6));
	else if (url.indexOf('approvalwaitlist.chn')>0)
		showcontextmenu(new Array(0,1,5,7,6,8));
	else if (url.indexOf('dellist.chn')>0)
		showcontextmenu(new Array(0,1,9));
	else if (url.indexOf('backlist.chn')>0)
		showcontextmenu(new Array(0,1,4,5,6));
	else if (url.indexOf('approvinglist.chn')>0)
		showcontextmenu(new Array(0,1,5,7,6,8));
	else if (url.indexOf('sendlist.chn')>0 || url.indexOf('sendapprovallist.chn')>0)
	{
		var tem = new Array(1); tem[0] = 0;
		showcontextmenu(tem);
	}
	
	if(published == 'false')
	{
	    hideContextMenu(8);
	}
}
function showcontextmenu(index)
{
	var i=0, count = index.length;
	var lis = document.getElementById('contextmenu').getElementsByTagName('li');
	for (i=0; i<count; i++)
		lis[index[i]].style.display = 'block';
}

function hideContextMenu(index)
{
    var lis = document.getElementById('contextmenu').getElementsByTagName('li');
    lis[index].style.display = 'none';
}

ie ?  window.attachEvent('onload', init_contextmenu) : window.addEventListener('load', init_contextmenu, false);
//*****************************************************************************************//
        
ie ?  window.attachEvent('onload', newslist_init) : window.addEventListener('load', newslist_init, false);
ie ?  document.attachEvent('onmouseup', newslist_mouseup) : document.addEventListener('mouseup', newslist_mouseup, false);
